package com.example.user.controller;


import com.example.user.common.Result;
import com.example.user.request.CartRequest;
import com.example.user.response.CartResponse;
import com.example.user.service.CartService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "http://localhost:8080")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 购物车列表展示
     */

    @GetMapping("/cartSelectPage")
    public Result<PageInfo> cartSelectPage(@RequestParam(defaultValue = "1") Integer userId, @RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "10") Integer pageSize) {
        
        PageHelper.startPage(pageNumber,pageSize);
       List<CartResponse>  cartResponseList=cartService.cartSelectPage(userId);
       return Result.success(new PageInfo<>(cartResponseList));
    }

    @PutMapping("/updateProductQuantity")
    public Result updateProductQuantity(@RequestBody CartRequest cartRequest) {
       Result result= cartService.updateProductQuantity(cartRequest);
       return result;
    }

    /**
     * 移除购物车操作
     */

    @DeleteMapping("/cancelCart/{id}")
    public Result cancelCart(@PathVariable Integer id)
    {
        Result result=cartService.cancelCart(id);
        return result;
    }

    /**
     * 商品结算接口
     */

    @PostMapping("/calculateTotal")
    public Result calculateTotal(@RequestBody List<Integer> itemIds)
    {
        Result result=cartService.calculateTotal(itemIds);
        return new Result<>(result);
    }

    /**
     * 生成订单接口
     */

    @PostMapping("/createOrder")
    public Result createOrder(@RequestBody CartRequest cartRequest)
    {
      Result result=  cartService.createOrder(cartRequest);
      return result;
    }


}

